package com.igoal.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.UserActivityDTO;
import com.igoal.utils.ConvertUtils;

@Controller
@RequestMapping("/ajax/secure/userNotification.html")
public class AjaxUserNotificationViewController extends BaseAjaxController{
	private final static Log logger = LogFactory.getLog(AjaxUserNotificationViewController.class);
	
	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap, @RequestParam("lastOrderValue") String lastOrderValue){
		String ownerName = null;
		if(isLoginUser()){
			ownerName = getUserInSession();
		}
		
		List<UserActivityDTO> notifications = new ArrayList<UserActivityDTO>();
		
		try{
			long lastValue = ConvertUtils.toLong(lastOrderValue, -1l);
			Date lastItemUpdateDate = null;
			if(lastValue > 0l){
				lastItemUpdateDate = new Date(lastValue);
			}
			
			notifications = actionLoggerService.getNewNotificationsLog(ownerName, lastItemUpdateDate, false);			
		}catch (Exception e) {
			logger.error(e);
		}
		
		applyModel(modelMap, notifications);
		return getViewPrefix() + "ajaxUserNotification";
	}
}
